import React, { FC } from 'react';
import { Button } from 'antd';
import { useLocation, useNavigate } from 'react-router-dom';
import Edit from './edit';
import AuditModal from '../component/auditModal';
import { getWorkFlowStatus } from '@/service';

const Groups: FC = () => {
  const location = useLocation();
  const navigate = useNavigate();
  const [showAuditResone, setShowAuditResone] = React.useState(false);
  const [isAudit, setIsAudit] = React.useState(false);
  const init = async () => {
    const res = await getWorkFlowStatus(location.state.process_instance_id);
    if (res.success) {
      setIsAudit(['pending', 'progressing'].includes(res.data));
    }
  };
  React.useEffect(() => {
    init();
  }, []);
  const auditPath = () => {
    return location.pathname.replace(/\/details\/\d+/, '');
  };
  return (
    <>
      <AuditModal
        onCancel={() => setShowAuditResone(false)}
        show={showAuditResone}
        onFinish={() => {
          setShowAuditResone(false);
          init();
        }}
      />
      <Edit
        isEdit={false}
        buttonList={[
          isAudit ? (
            <Button style={{ display: 'inline' }} type="primary" onClick={() => setShowAuditResone(true)}>
              审核
            </Button>
          ) : (
            ''
          ),
          <Button
            onClick={() => {
              navigate(`${auditPath()}`);
            }}
          >
            返回
          </Button>,
        ]}
      />
    </>
  );
};

export default Groups;
